package com.wicture.erp4android.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wicture.erp4android.R;
import com.wicture.erp4android.ui.minterface.AutoEquit;
import com.wicture.erp4android.utils.EquitDelay;

public class MainAct extends FragmentActivity implements OnClickListener,AutoEquit{


public int step1 = 0;
public int step2 = 0;
private int time=0;

private int equit=0;//控制常量 用来控制返回键退出

private GridView gv_functions;
private boolean start ;

private int[] imageResource = {R.drawable.th_xplane,R.drawable.th_xplane_blue,R.drawable.th_xplane_green,
		R.drawable.th_xplane_purple,R.drawable.th_xplane_red,R.drawable.th_camera};
private String[] functionNames = {"复核","核单","打印","称重","回收","拣货"};


private Handler handler = new Handler(){
	public void handleMessage(android.os.Message msg) {
		switch (msg.what) {
		case 0:
time++;
if(time<30*60){
	if(start){
		handler.sendEmptyMessageDelayed(0, 1000);
	}else{
		time = 0;

		
	}
}else{
//	start =true;
	unRegist();
	finish();
	time = 0;
}
			break;

		default:
			break;
		}
	};
	
};
private EquitDelay equitDelay;
private Thread thread;
private SharedPreferences sp;
private TextView tv_operator;
/**
 * 初始化界面
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	
	setContentView(R.layout.act_main);
	sp = getSharedPreferences("config", MODE_PRIVATE);
	String operatorname = sp.getString("userName", null);
	 gv_functions = (GridView) findViewById(R.id.gv_functions);
	 tv_operator = (TextView) findViewById(R.id.tv_operator);
	 tv_operator.setText(operatorname);
	 gv_functions.setAdapter(new MyAdapter());
	 gv_functions.setOnItemClickListener(new OnItemClickListener() {

		  
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (position) {
			case 0:
				//打开复核界面
				Intent intent1 = new Intent(MainAct.this,SortingAct.class);
				startActivity(intent1);
				break;
			case 1:
				Intent intent2 = new Intent(MainAct.this,RecheckAct.class);
				startActivity(intent2);
				break;
			case 2:
				Intent intent3 = new Intent(MainAct.this,PrintAct.class);
				startActivity(intent3);
				break;
			case 3:
				//打开称重的界面
				Intent intent4 = new Intent(MainAct.this,WeightingAct.class);
				startActivity(intent4);
				break;
			case 4:
				Intent intent5 = new Intent(MainAct.this,DebindFboxAct.class);
				startActivity(intent5);
				break;
			case 5:
				Intent intent6 = new Intent(MainAct.this,PickingFinishedGoods.class);
				startActivity(intent6);
			default:
				break;
			}
			
		}
	});
equitDelay = new EquitDelay(this);
}


@Override
	public void onAttachedToWindow() {
		// TODO Auto-generated method stub
	
		super.onAttachedToWindow();
	}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

	if(keyCode==KeyEvent.KEYCODE_BACK){
		if(equit==0){
			equit++;
		Toast.makeText(MainAct.this, "再点一次退出", Toast.LENGTH_SHORT).show();
		new Thread(){

			@Override
			public void run() {
				try {
					Thread.sleep(3000);
					equit=0;
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}.start();
		return true;
		}else if(equit==1){
			equit = 0;
			
			SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
			Editor edit = sp.edit();
			edit.clear();
			edit.commit();
			return super.onKeyDown(keyCode, event);
		}
		
	}
	return super.onKeyDown(keyCode, event);
}

@Override
public void onClick(View v) {
	// TODO Auto-generated method stub
	
}


private class MyAdapter extends BaseAdapter{

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return functionNames.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = View.inflate(MainAct.this, R.layout.item_function, null);
		TextView tv_function = (TextView) view.findViewById(R.id.tv_function);
		ImageView iv_function = (ImageView) view.findViewById(R.id.iv_function);
		tv_function.setText(functionNames[position]);
		iv_function.setImageResource(imageResource[position]);
		return view;
	}
	
}

private void autoEquit(){
//	handler.postDelayed(new Runnable() {
//		
//		@Override
//		public void run() {
//			EquitDelay.equit();
//			
//		}
//	}, 4000);
	System.out.println("开始退出");
//	time++;
	
	if(start){
	handler.postDelayed(new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
		
			start = false;
			handler.sendEmptyMessage(0);
		}
	}, 4000);
	}
	
}

@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		equit();
	}
@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
//		equitDelay.stopEquit();
		cancleEquit();
	
	}


@Override
public void equit() {
	start = true;
	handler.sendEmptyMessage(0);
//	// TODO Auto-generated method stub
//	thread = new Thread(new Runnable() {
//		
//		@Override
//		public void run() {
//
//			synchronized (MainAct.this) {
//				try {
//					wait(4000);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				if(start){
//					finish();
//					
//					}
//				start = true;
//			}
//		
//		
//		}
//	});
//thread.start();
}


@Override
public void cancleEquit() {
	// TODO Auto-generated method stub
//	synchronized (this) {
//		
//	start = false;
//	if(thread!=null&&thread.isAlive()){
//	thread.notify();
//	}
//	}
//	
	start = false;
}

private void unRegist(){
	
	Editor edit = sp.edit();
	edit.clear();
	edit.commit();
	
}

}
